package cn.flying.cloud.sso.server.configuration.security;

import javax.annotation.PostConstruct;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;


/**
 * 自定义动态加载用户信息数据服务
 *
 * @author: admin
 * @date: 2022年11月07日 20:57
 * @version: 1.0
 */
@Component
public class SecurityUserDetailService implements UserDetailsService {

    private List<User> userList;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostConstruct
    public void initData() {
        String password = passwordEncoder.encode("123456");
        userList = new ArrayList<>();
        userList.add(new User("macro", password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin")));
        userList.add(new User("andy", password, AuthorityUtils.commaSeparatedStringToAuthorityList("client")));
        userList.add(new User("mark", password, AuthorityUtils.commaSeparatedStringToAuthorityList("client")));
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<User> findUserList = userList.stream().filter(user -> user.getUsername().equals(username)).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(findUserList)) {
            return findUserList.get(0);
        } else {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
    }

//    @Resource
//    private SysUserService sysUserService;
//
//    /**
//     * 加载用户信息
//     *
//     * @param username
//     * @return
//     * @throws UsernameNotFoundException
//     */
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        SecurityUser securityUser = sysUserService.findUserByUsername(username);
//        if (securityUser == null) {
//            throw new UsernameNotFoundException("用户不存在");
//        }
//
//        //加载用户的角色权限
//        List<SysUserRole> list = sysUserService.getUserRoleList(securityUser.getUserId());
//        if (CollectionUtils.isEmpty(list)) {
//            return securityUser;
//        }
//
//        List<String> authorities = new ArrayList<>();
//        //加载用户所有的权限资源
//        List<SysResource> resources = sysUserService.getUserResourceList(securityUser.getUserId());
//        List<String> res = resources.stream().map(sysResource -> "R" + sysResource.getResId()).collect(Collectors.toList());
//        authorities.addAll(res);
//
//        List<String> mid = resources.stream().map(SysResource::getMid).collect(Collectors.toList());
//        authorities.addAll(mid);
//
//        //处理角色，增加前缀“ROLE_”
//        List<String> roles = list.stream().map(sysUserRole -> "ROLE_" + sysUserRole.getRoleId()).collect(Collectors.toList());
//        authorities.addAll(roles);
//
//        //包装当前用户的权限到
//        securityUser.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(String.join(",", authorities)));
//        return securityUser;
//    }
}
